﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Nova_DAL;
using Nova_Common;
using System.Data.Common;
using System.Data;
using System.Collections;

public partial class Storage_Storage_Fill : System.Web.UI.Page
{
    DbHelper dbHelper = new DbHelper();
    DbCommand cmd = null;
    string strSQL = "";
    DataTable dtone_storage = null;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (hd.Value.ToString().Trim() == "")
        {
            if (Session["type_dtone"] == null)
            {
                Session["dtone_storage"] = null;
            }
            else
            {
                Session["type_dtone"] = null;
            }
        }

        if (this.hidden.Value != "" && this.hidden.Value != "undefined")//判断是否是模态关闭后刷新页面
        {

            if (Session["dtone_storage"] == null)
            {
                tempDtone();
            }
            else
            {
                GridViewBind();
            }
            hidden.Value = "";
        }

        if (!IsPostBack)
        {
            ddlBind();
            ddl_Bind();
        }
    }
    protected void ddlBind()
    {
        strSQL = "SELECT No_Lane FROM TBase_Position WHERE No_Lane !='0' Group by No_Lane ORDER BY No_Lane";
        cmd = dbHelper.GetSqlStringCommond(strSQL);
        ddlLane.DataSource = dbHelper.ExecuteDataSet(cmd);
        ddlLane.DataBind();
        ddlLane.Items.Insert(0, new ListItem("巷道选择", "0"));

        strSQL = "SELECT No_Line FROM TBase_Position WHERE No_Line !='0' Group by No_Line ORDER BY No_Line";
        cmd = dbHelper.GetSqlStringCommond(strSQL);
        ddlLine.DataSource = dbHelper.ExecuteDataSet(cmd);
        ddlLine.DataBind();
        ddlLine.Items.Insert(0, new ListItem("排选择", "0"));

        strSQL = "SELECT No_Col FROM TBase_Position WHERE No_Col !='0' Group by No_Col ORDER BY No_Col";
        cmd = dbHelper.GetSqlStringCommond(strSQL);
        ddlCol.DataSource = dbHelper.ExecuteDataSet(cmd);
        ddlCol.DataBind();
        ddlCol.Items.Insert(0, new ListItem("列选择", "0"));

        strSQL = "SELECT No_Layer FROM TBase_Position WHERE No_Layer !='0' Group by No_Layer ORDER BY No_Layer";
        cmd = dbHelper.GetSqlStringCommond(strSQL);
        ddlLayer.DataSource = dbHelper.ExecuteDataSet(cmd);
        ddlLayer.DataBind();
        ddlLayer.Items.Insert(0, new ListItem("层选择", "0"));

        strSQL = "SELECT SQIDE FROM ECSStandard..TSQLanePath WHERE TaskMode in (2,4) Group by SQIDE ORDER BY SQIDE";
        cmd = dbHelper.GetSqlStringCommond(strSQL);
        ddlSQLIDE.DataSource = dbHelper.ExecuteDataSet(cmd);
        ddlSQLIDE.DataBind();
        ddlSQLIDE.Items.Insert(0, new ListItem("设备选择", "0"));
    }

    /// <summary>
    /// 设备号
    /// </summary>
    protected void ddl_Bind()
    {
        strSQL = "SELECT SQIDE FROM ECSStandard..TSQLanePath WHERE TaskMode in (4) Group by SQIDE ORDER BY SQIDE";
        cmd = dbHelper.GetSqlStringCommond(strSQL);
        ddlSQLIDE.DataSource = dbHelper.ExecuteDataSet(cmd);
        ddlSQLIDE.DataBind();
        ddlSQLIDE.Items.Insert(0, new ListItem("设备选择", "0"));
    }

    /// <summary>
    /// 创建虚拟表
    /// </summary>
    private void tempDtone()
    {
        dtone_storage = new DataTable("dtone_storage");
        dtone_storage.Columns.Add("id", System.Type.GetType("System.Int32"));
        dtone_storage.Columns.Add("ck", System.Type.GetType("System.Boolean"));
        dtone_storage.Columns.Add("Tray_ID", System.Type.GetType("System.String"));
        dtone_storage.Columns.Add("Pos_ID", System.Type.GetType("System.String"));
        dtone_storage.Columns.Add("Mat_Code", System.Type.GetType("System.String"));
        dtone_storage.Columns.Add("Mat_Name", System.Type.GetType("System.String"));
        dtone_storage.Columns.Add("Sec_Mat_ID", System.Type.GetType("System.String"));
        dtone_storage.Columns.Add("Mat_Num", System.Type.GetType("System.Decimal"));
        dtone_storage.Columns.Add("Num", System.Type.GetType("System.Decimal"));
        //Session["dtone_storage"] = dtone_storage;
    }

    /// <summary>
    /// 绑定虚拟表
    /// </summary>
    private void GridViewBind()
    {
        hd.Value = "1";
        dtone_storage = (DataTable)Session["dtone_storage"];
        if (dtone_storage == null)
        {
            tempDtone();
        }

        Session["dtone_storage"] = dtone_storage;

        gv.DataSource = dtone_storage;
        gv.DataBind();
    }



    /// <summary>
    /// 绑定虚拟表
    /// </summary>
    private void GridView_Bind()
    {
        DataSet ds = null;

        dtone_storage = (DataTable)Session["dtone_storage"];
        if (dtone_storage == null)
        {
            tempDtone();
        }
        ds = getSQL();


        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            string strTray_ID = ds.Tables[0].Rows[i]["Tray_ID"].ToString();
            string strSec_Mat_ID = ds.Tables[0].Rows[i]["Sec_Mat_ID"].ToString();
            int iFlag = 0;

            for (int j = 0; j < dtone_storage.Rows.Count; j++)
            {
                if (dtone_storage.Rows[j]["Tray_ID"].ToString() == strTray_ID && dtone_storage.Rows[j]["Sec_Mat_ID"].ToString() == strSec_Mat_ID)
                {
                    DataRow drr = dtone_storage.Rows[Convert.ToInt32(j)];
                    drr.BeginEdit();
                    drr["Mat_Num"] = Convert.ToDecimal(ds.Tables[0].Rows[i]["MAT_NUM"].ToString());
                    drr.EndEdit();
                    Session["dtone_storage"] = dtone_storage;
                    iFlag = 1;
                }
            }
            if (iFlag == 0)
            {
                DataRow dr = dtone_storage.NewRow();
                if (dtone_storage.Rows.Count == 0)
                {
                    dr["id"] = 1;
                }
                else
                {
                    dr["id"] = dtone_storage.Rows.Count + 1;
                }
                dr["ck"] = false;
                dr["Tray_ID"] = ds.Tables[0].Rows[i]["Tray_ID"].ToString();
                dr["Pos_ID"] = ds.Tables[0].Rows[i]["Pos_ID"].ToString();
                dr["Mat_Code"] = ds.Tables[0].Rows[i]["Mat_Code"].ToString();
                dr["Mat_Name"] = ds.Tables[0].Rows[i]["Mat_Name"].ToString();
                dr["Sec_Mat_ID"] = ds.Tables[0].Rows[i]["Sec_Mat_ID"].ToString();
                dr["Mat_Num"] = ds.Tables[0].Rows[i]["Mat_Num"].ToString();
                dr["Num"] = 0;
                dtone_storage.Rows.Add(dr);
                Session["dtone_storage"] = dtone_storage;
                iFlag = 0;
            }

        }
       
        gv.DataSource = dtone_storage;
        gv.DataBind();
    }



    /// <summary>
    /// 数据库里的数据
    /// </summary>
    /// <returns></returns>
    protected DataSet getSQL()
    {
        string WCode = Request.Cookies["WType"].Value;
        strSQL = @"SELECT TRP.TRAY_ID,TBP.POS_ID, ISNULL(TBM.MAT_CODE,'') MAT_CODE,ISNULL(TBM.MAT_NAME,'') MAT_NAME,ISNULL(TBS.MAT_NUM,0) MAT_NUM,TBM.S01,TBI.SEC_MAT_ID
                                FROM TRelations TRP 
	                            LEFT JOIN TBase_Position TBP ON TBP.POS_ID=TRP.POS_ID
	                            LEFT JOIN TBase_TrayPos TBT ON TBT.TRAY_ID=TRP.TRAY_ID
	                            LEFT JOIN TRel_TrayPosContainer TRT ON TRT.RELATIONS_ID=TBT.RELATIONS_ID
	                            LEFT JOIN TBase_Storage TBS	ON TBS.CONTAINERID=TRT.CONTAINERID
	                            LEFT JOIN TBase_Interface TBI ON TBI.SEC_MAT_ID=TBS.SEC_MAT_ID
	                            LEFT JOIN TBase_Material TBM ON TBM.MAT_CODE=TBI.MAT_CODE
	                            LEFT JOIN TBase_Zone TBZ ON TBZ.ZONE_ID=TBP.ZONE_ID WHERE TBZ.STRUCTURE=1  and (TBI.S01='" + WCode + "' or 'FSSC'='" + WCode + "')";
        if (txt_Tray_ID.Text.ToString().Trim() != "")
        {
            strSQL += " AND TRP.Tray_ID = '" + txt_Tray_ID.Text.ToString().Trim() + "'";
        }
        if (txt_Mat_Code.Text.ToString().Trim() != "")
        {
            strSQL += " AND TBM.Mat_Code like '%" + txt_Mat_Code.Text.ToString().Trim() + "%'";
        }
        if (ddlLane.SelectedValue != "0")
        {
            strSQL += " AND TBP.No_Lane = '" + ddlLane.SelectedValue.ToString() + "'";
        }
        if (ddlLine.SelectedValue != "0")
        {
            strSQL += " AND TBP.No_Line = '" + ddlLine.SelectedValue.ToString() + "'";
        }

        if (ddlCol.SelectedValue != "0")
        {
            strSQL += " AND TBP.No_Col = '" + ddlCol.SelectedValue.ToString() + "'";
        }
        if (ddlLayer.SelectedValue != "0")
        {
            strSQL += " AND TBP.No_Layer = '" + ddlLayer.SelectedValue.ToString() + "'";
        }
        strSQL += " ORDER BY TBP.PID ";

        cmd = dbHelper.GetSqlStringCommond(strSQL);
        DataSet ds = dbHelper.ExecuteDataSet(cmd);
        return ds;
    }


    /// <summary>
    /// 查询
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ibtn_Search_Click(object sender, ImageClickEventArgs e)
    {
        hd.Value = "1";
        ///清空数据
        //Session["dtone_storage"] = null;
        if (dtone_storage == null)
        {
            tempDtone();
        }
        GridView_Bind();
        Label1.Text = "";
    }

    protected void txt_Num_TextChanged(object sender, EventArgs e)
    {
        dtone_storage = (DataTable)Session["dtone_storage"];
        TextBox txt = (TextBox)sender;
        for (int i = 0; i <= dtone_storage.Rows.Count; i++)
        {
            DataRow dr = dtone_storage.Rows[i];
            if (i == Convert.ToInt32(txt.ToolTip.ToString()) - 1)
            {
                dr.BeginEdit();
                dr["Num"] = txt.Text.ToString().Trim();
                dr.EndEdit();
                break;
            }
        }
    }


    protected void ibtn_ZX_Click(object sender, ImageClickEventArgs e)
    {
        Label1.Text = "";
        int iResNo = 0;
        string strResMsg = "";
        string strTRAY_ID = "";

        if (ddlSQLIDE.SelectedValue == "0")
        {
            ShowBox.MessageBox(this.Page, "请选择设备号！");
            return;
        }

        ///清空临时表
        cmd = dbHelper.GetStoredProcCommond("SP_START_OUTPUT");
        dbHelper.ExecuteNonQuery(cmd);

        for (int i = 0; i < gv.Rows.Count; i++)
        {
            CheckBox chkSelect = (CheckBox)gv.Rows[i].Cells[0].FindControl("chkSelect");
            TextBox txt = (TextBox)gv.Rows[i].FindControl("txt_Num");
            if (chkSelect.Checked)
            {
                ///组盘
                string strSEC_MAT_ID = Package(i);
                if (strSEC_MAT_ID != "-1")
                {
                    string strSQL = @"UPDATE TRelations SET POS_ID=@POS_ID WHERE PID IN(SELECT TRP.PID FROM TBase_TrayPos TBT 
	                            INNER JOIN TRel_TrayPosContainer TRT ON TRT.RELATIONS_ID=TBT.RELATIONS_ID
	                            INNER JOIN TBase_Storage TBS ON TBS.CONTAINERID=TRT.CONTAINERID
	                            INNER JOIN TRelations TRP ON TRP.TRAY_ID=TBT.TRAY_ID 
                                WHERE TBT.TRAY_ID=@TRAY_ID AND TBS.SEC_MAT_ID = @SEC_MAT_ID)";

                    cmd = dbHelper.GetSqlStringCommond(strSQL);
                    dbHelper.AddInParameter(cmd, "@POS_ID", DbType.String, gv.Rows[i].Cells[3].Text.ToString().Trim());
                    dbHelper.AddInParameter(cmd, "@TRAY_ID", DbType.String, gv.Rows[i].Cells[2].Text.ToString().Trim());
                    dbHelper.AddInParameter(cmd, "@SEC_MAT_ID", DbType.String, strSEC_MAT_ID);
                    dbHelper.ExecuteNonQuery(cmd);

                    strSQL = @"SELECT TBS.PID AS STORE_PID FROM TBase_Storage TBS 
	                            LEFT JOIN TRel_TrayPosContainer TRT ON TRT.CONTAINERID=TBS.CONTAINERID
	                            LEFT JOIN TBase_TrayPos TBT ON TBT.RELATIONS_ID=TRT.RELATIONS_ID WHERE TBS.SEC_MAT_ID=@SEC_MAT_ID AND TBT.TRAY_ID=@TRAY_ID";
                    cmd = dbHelper.GetSqlStringCommond(strSQL);
                    dbHelper.AddInParameter(cmd, "@SEC_MAT_ID", DbType.String, strSEC_MAT_ID);
                    dbHelper.AddInParameter(cmd, "@TRAY_ID", DbType.String, gv.Rows[i].Cells[2].Text.ToString().Trim());
                    DataSet ds = dbHelper.ExecuteDataSet(cmd);
                    if (ds != null && ds.Tables[0].Rows.Count > 0)
                    {
                        ///插入临时表
                        cmd = dbHelper.GetStoredProcCommond("[SP_INSERT_FOR_OUT]");
                        dbHelper.AddInParameter(cmd, "@STORE_PID", DbType.String, ds.Tables[0].Rows[0]["STORE_PID"].ToString());
                        dbHelper.AddInParameter(cmd, "@MAT_NUM", DbType.String, txt.Text.Trim());
                        dbHelper.AddInParameter(cmd, "@DETAIL_PID", DbType.String, gv.Rows[i].Cells[3].Text.ToString());
                        dbHelper.ExecuteNonQuery(cmd);
                    }

                    strTRAY_ID = gv.Rows[i].Cells[2].Text.ToString().Trim();
                }
                else
                {
                    strTRAY_ID = "";
                    Label1.Text = "补货失败！";
                    //Del_Stroage();
                }
            }
        }

        if (strTRAY_ID != "")
        {
            cmd = dbHelper.GetStoredProcCommond("[SP_Generate_Not_IN_CMDs_By_Pos_DEFINE]");
            dbHelper.AddInParameter(cmd, "@TRAY_ID", DbType.String, strTRAY_ID);
            dbHelper.AddInParameter(cmd, "@SQIDE", DbType.Int32, ddlSQLIDE.SelectedValue.ToString());
            dbHelper.AddInParameter(cmd, "@TASKMODE", DbType.Int32, 7);
            dbHelper.AddInParameter(cmd, "@MEMO", DbType.String, "");
            dbHelper.AddInParameter(cmd, "@User_Name", DbType.String, Request.Cookies["vNovaUserNo"].Value.ToString());
            dbHelper.AddOutParameter(cmd, "@RESNO", DbType.Int32, 100);
            dbHelper.AddOutParameter(cmd, "@ResMsg", DbType.String, 255);
            dbHelper.ExecuteNonQuery(cmd);
            iResNo = Convert.ToInt32(dbHelper.GetParameter(cmd, "@RESNO").Value.ToString());
            strResMsg = dbHelper.GetParameter(cmd, "@ResMsg").Value.ToString();

            if (iResNo > 0)
            {
                Label1.Text = "操作成功！";

                Seach();
            }
            else
            {
                Label1.Text = strResMsg.ToString();
                //Del_Stroage();
            }
        }
    }


    /// <summary>
    /// 组盘
    /// </summary>
    /// <param name="i"></param>
    protected string Package(int i)
    {
        try
        {
            TextBox txt = (TextBox)gv.Rows[i].FindControl("txt_Num");
            string strSec_Mat_ID = getSec_Mat_ID(i);
            cmd = dbHelper.GetStoredProcCommond("SP_PACKAGE_ALL_100X");
            dbHelper.AddInParameter(cmd, "@TRAY_ID", DbType.String, gv.Rows[i].Cells[2].Text.ToString().Trim());
            dbHelper.AddInParameter(cmd, "@SEC_MAT_ID", DbType.Int64, strSec_Mat_ID);
            dbHelper.AddInParameter(cmd, "@MAT_NUM", DbType.Double, Convert.ToDouble(txt.Text.ToString().Trim()));
            dbHelper.AddInParameter(cmd, "@USERNAME", DbType.String, Request.Cookies["vNovaUserNo"].Value.ToString());
            dbHelper.AddOutParameter(cmd, "@ResNO", DbType.Int32, 100);
            dbHelper.AddOutParameter(cmd, "@RESMsg", DbType.String, 255);
            dbHelper.ExecuteNonQuery(cmd);
            int iResNo = Convert.ToInt32(dbHelper.GetParameter(cmd, "@RESNO").Value.ToString());
            string strResMsg = dbHelper.GetParameter(cmd, "@RESMsg").Value.ToString();

            if (iResNo > 0)
            {
                strResMsg = "组盘成功！";
                return strSec_Mat_ID;
            }
            else
            {
                return "-1";
            }
        }
        catch (Exception ex)
        {
            return "-1";
        }
    }


    /// <summary>
    /// 生成二级编码
    /// </summary>
    /// <param name="i"></param>
    /// <returns></returns>
    protected string getSec_Mat_ID(int i)
    {

        HttpCookie WCode = Request.Cookies["WCode"];
        HttpCookie WName = Request.Cookies["WName"];

        string strMat_Code = gv.Rows[i].Cells[5].Text.ToString().Trim();
        cmd = dbHelper.GetStoredProcCommond("SP_Get_Sec_Mat_ID");
        dbHelper.AddInParameter(cmd, "@Mat_Code", DbType.String, strMat_Code);
        dbHelper.AddInParameter(cmd, "@D01", DbType.String, "");
        dbHelper.AddInParameter(cmd, "@D02", DbType.String, "");
        dbHelper.AddInParameter(cmd, "@D03", DbType.String, "");
        dbHelper.AddInParameter(cmd, "@D04", DbType.String, "");
        dbHelper.AddInParameter(cmd, "@D05", DbType.String, "");
        dbHelper.AddInParameter(cmd, "@D06", DbType.String, "");
        dbHelper.AddInParameter(cmd, "@D07", DbType.String, "");
        dbHelper.AddInParameter(cmd, "@D08", DbType.String, "");
        dbHelper.AddInParameter(cmd, "@D09", DbType.String, "");
        dbHelper.AddInParameter(cmd, "@D10", DbType.String, "");
        dbHelper.AddInParameter(cmd, "@S01", DbType.String, "");
        dbHelper.AddInParameter(cmd, "@S02", DbType.String, "");
        dbHelper.AddInParameter(cmd, "@S03", DbType.String, "");
        dbHelper.AddInParameter(cmd, "@S04", DbType.String, "");
        dbHelper.AddInParameter(cmd, "@S05", DbType.String, "");
        dbHelper.AddInParameter(cmd, "@S06", DbType.String, "");
        dbHelper.AddInParameter(cmd, "@S07", DbType.String, "");
        dbHelper.AddInParameter(cmd, "@S08", DbType.String, "");
        dbHelper.AddInParameter(cmd, "@S09", DbType.String, "");
        dbHelper.AddInParameter(cmd, "@S10", DbType.String, "");
        dbHelper.AddInParameter(cmd, "@S11", DbType.String, "");
        dbHelper.AddInParameter(cmd, "@S12", DbType.String, "");
        dbHelper.AddInParameter(cmd, "@S13", DbType.String, "");
        dbHelper.AddInParameter(cmd, "@S14", DbType.String, "");
        dbHelper.AddInParameter(cmd, "@S15", DbType.String, "");
        dbHelper.AddInParameter(cmd, "@S16", DbType.String, "");
        dbHelper.AddInParameter(cmd, "@S17", DbType.String, "");
        dbHelper.AddInParameter(cmd, "@S18", DbType.String, "");
        dbHelper.AddInParameter(cmd, "@S19", DbType.String, "");
        dbHelper.AddInParameter(cmd, "@S20", DbType.String, "");
        dbHelper.AddInParameter(cmd, "@N01", DbType.Decimal, 0);
        dbHelper.AddInParameter(cmd, "@N02", DbType.Decimal, 0);
        dbHelper.AddInParameter(cmd, "@N03", DbType.Decimal, 0);
        dbHelper.AddInParameter(cmd, "@N04", DbType.Decimal, 0);
        dbHelper.AddInParameter(cmd, "@N05", DbType.Decimal, 0);
        dbHelper.AddInParameter(cmd, "@N06", DbType.Decimal, 0);
        dbHelper.AddInParameter(cmd, "@N07", DbType.Decimal, 0);
        dbHelper.AddInParameter(cmd, "@N08", DbType.Decimal, 0);
        dbHelper.AddInParameter(cmd, "@N09", DbType.Decimal, 0);
        dbHelper.AddInParameter(cmd, "@N10", DbType.Decimal, 0);
        dbHelper.AddInParameter(cmd, "@DynamicState01", DbType.Int16, 0);
        dbHelper.AddInParameter(cmd, "@DynamicState02", DbType.Int16, 0);
        dbHelper.AddInParameter(cmd, "@DynamicState03", DbType.Int16, 0);
        dbHelper.AddInParameter(cmd, "@DynamicState04", DbType.Int16, 0);
        dbHelper.AddInParameter(cmd, "@DynamicState05", DbType.Int16, 0);
        dbHelper.AddInParameter(cmd, "@TS01", DbType.String, "");
        dbHelper.AddInParameter(cmd, "@TS02", DbType.String, "");
        dbHelper.AddInParameter(cmd, "@TS03", DbType.String, "");
        dbHelper.AddInParameter(cmd, "@TS04", DbType.String, "");
        dbHelper.AddInParameter(cmd, "@TS05", DbType.String, "");
        dbHelper.AddInParameter(cmd, "@ForceNew", DbType.Int16, 1);
        dbHelper.AddInParameter(cmd, "@UserName", DbType.String, "");
        dbHelper.AddOutParameter(cmd, "@Sec_Mat_ID", DbType.Int32, 20);
        dbHelper.AddOutParameter(cmd, "@ResNO", DbType.Int16, 10);
        dbHelper.AddOutParameter(cmd, "@ResMsg", DbType.String, 255);
        dbHelper.ExecuteNonQuery(cmd);
        int iSecs = Convert.ToInt32(dbHelper.GetParameter(cmd, "@Sec_Mat_ID").Value.ToString());
        string strSecs = iSecs.ToString();
        return strSecs;
    }


    protected void Del_Stroage()
    {
        strSQL = "SELECT * FROM TmpGenerateOutCMDs_Define";
        cmd = dbHelper.GetSqlStringCommond(strSQL);
        DataSet ds_toc = dbHelper.ExecuteDataSet(cmd);
        if (ds_toc != null)
        {
            for (int i = 0; i < ds_toc.Tables[0].Rows.Count; i++)
            {
                strSQL = "UPDATE TBase_Storage SET MAT_NUM =MAT_NUM-'" + ds_toc.Tables[0].Rows[i]["MAT_NUM"].ToString() + "' WHERE PID='" + ds_toc.Tables[0].Rows[i]["STORE_PID"].ToString() + "'";
                cmd = dbHelper.GetSqlStringCommond(strSQL);
                dbHelper.ExecuteNonQuery(cmd);
            }
        }
    }


    protected void ibrn_S_Click(object sender, ImageClickEventArgs e)
    {
        hd.Value = "1";
        HttpCookie WType = Request.Cookies["WType"];

        strSQL = @"SELECT TAS.PID,TAS.STOCKNO,TAS.MAT_CODE,TBM.MAT_NAME,TAS.ACT_NUM,TAS.NUM,TAS.SEC_MAT_ID,TBM.S01,TBT.TRAY_ID,TR.POS_ID 
						FROM dbo.TAddStorage_List AS TAS 
                        LEFT JOIN TBase_Interface TBI ON TAS.Sec_Mat_ID=TBI.Sec_Mat_ID
                        LEFT JOIN TBase_Material TBM ON TAS.MAT_CODE=TBM.MAT_CODE 
						LEFT JOIN TBase_Storage TBS ON TBS.Sec_Mat_ID=TBI.Sec_Mat_ID
						LEFT JOIN TRel_TrayPosContainer TRT ON TRT.CONTAINERID=TBS.CONTAINERID
						LEFT JOIN TBase_TrayPos TBT ON TBT.RELATIONS_ID=TRT.RELATIONS_ID
						LEFT JOIN TRelations TR ON TR.TRAY_ID=TBT.TRAY_ID
                            WHERE TAS.StockNo='" + txt_StorageNo.Text.ToString().Trim() + "' AND (TBI.S01 = '" + WType.Value.ToString().Trim() + "' OR 'FSSC'='" + WType.Value.ToString().Trim() + "')";
        cmd = dbHelper.GetSqlStringCommond(strSQL);
        GridView1.DataSource = dbHelper.ExecuteDataSet(cmd);
        GridView1.DataBind();
    }

    protected void lbtn_ZP_Click(object sender, EventArgs e)
    {
        hd.Value = "1";

        HttpCookie WType = Request.Cookies["WType"];
        LinkButton lbtnZP = (LinkButton)sender;
        string[] sArray = lbtnZP.CommandArgument.ToString().Split('|');
        string strSec_Mat_ID = sArray[1].ToString();
        string strNum = sArray[2].ToString();


        strSQL = @"SELECT TBT.Tray_ID,TBI.Mat_Code,TBS.Mat_Num FROM TBase_Storage TBS LEFT JOIN TBase_Interface TBI ON TBI.Sec_Mat_ID=TBS.Sec_Mat_ID
        	                    LEFT JOIN TRel_TrayPosContainer TRT ON TRT.ContainerID= TBS.ContainerID
        	                    LEFT JOIN TBase_TrayPos TBT ON TRT.Relations_ID=TBT.Relations_ID
                                LEFT JOIN TRelations TRP ON TRP.Tray_ID = TBT.Tray_ID 
                                LEFT JOIN TBase_Position TBP ON TRP.Pos_ID = TBP.Pos_ID 
        	                    WHERE TBI.Sec_Mat_ID='" + strSec_Mat_ID + "' AND TBP.Pos_ID!='200000000000'";

        cmd = dbHelper.GetSqlStringCommond(strSQL);
        DataSet ds_S = dbHelper.ExecuteDataSet(cmd);
        string strMat_Num = "0";
        if (ds_S != null && ds_S.Tables[0].Rows.Count > 0)
        {
            strMat_Num = ds_S.Tables[0].Rows[0]["Mat_Num"].ToString();
        }

        Add_Bind(sArray[3].ToString(), sArray[4].ToString(), sArray[0].ToString(), sArray[5].ToString(),strMat_Num,strNum, strSec_Mat_ID);


        gv.DataSource = dtone_storage;
        gv.DataBind();
    }

    protected void Add_Bind(string strTray_ID, string strPos_ID, string strMat_Code, string strMat_Name, string strMat_Num, string strNum, string strSec_Mat_ID)
    {
        dtone_storage = (DataTable)Session["dtone_storage"];
        if (dtone_storage == null)
        {
            tempDtone();
        }
        int iFlag = 0;
        for (int i = 0; i < dtone_storage.Rows.Count; i++)
        {
            if (dtone_storage.Rows[i]["Tray_ID"].ToString() == strTray_ID && dtone_storage.Rows[i]["Sec_Mat_ID"].ToString() == strSec_Mat_ID)
            {
                DataRow drr = dtone_storage.Rows[Convert.ToInt32(i)];
                drr.BeginEdit();
                drr["Num"] = Convert.ToDecimal(drr["Num"].ToString()) + Convert.ToDecimal(strNum.ToString());
                drr.EndEdit();

                Session["dtone_storage"] = dtone_storage;
                iFlag = 1;
            }
        }

        if (iFlag == 0)
        {
            DataRow dr = dtone_storage.NewRow();
            if (dtone_storage.Rows.Count == 0)
            {
                dr["id"] = 1;
            }
            else
            {
                dr["id"] = dtone_storage.Rows.Count + 1;
            }
            dr["ck"] = false;
            dr["Tray_ID"] = strTray_ID;
            dr["Pos_ID"] = strPos_ID;
            dr["Mat_Code"] = strMat_Code;
            dr["Mat_Name"] = strMat_Name;
            dr["Sec_Mat_ID"] = strSec_Mat_ID;
            dr["Mat_Num"] = strMat_Num;
            dr["Num"] = strNum;
            dtone_storage.Rows.Add(dr);

            Session["dtone_storage"] = dtone_storage;
            iFlag = 0;
        }
    }


    protected void Seach()
    {
        for (int i = 0; i < gv.Rows.Count; i++)
        {
            string str = gv.Rows[i].Cells[4].Text.ToString();

            strSQL = @"SELECT Sec_Mat_ID,Mat_Num FROM    TBase_Storage TBS 
                           LEFT JOIN TRel_TrayPosContainer TRT ON TBS.ContainerID=TRT.ContainerID 
                           LEFT JOIN TBase_TrayPos TBT ON TBT.Relations_ID = TRT.Relations_ID 
                           LEFT JOIN TRelations TRP ON TRP.Tray_ID = TBT.Tray_ID 
                           LEFT JOIN TBase_Position TBP ON TRP.Pos_ID = TBP.Pos_ID 
                            WHERE TBP.Pos_ID !='200000000000' AND Sec_Mat_ID='" + str + "' AND TBT.TRAY_ID='" + gv.Rows[i].Cells[2].Text.ToString() + "'";
            cmd = dbHelper.GetSqlStringCommond(strSQL);
            DataSet DS_m = dbHelper.ExecuteDataSet(cmd);
            gv.Rows[i].Cells[7].Text = DS_m.Tables[0].Rows[0]["Mat_Num"].ToString();
        }
    }
}
